package com.fairytask.extention;

import java.sql.SQLException;

import com.fairytask.extention.define.CustomToClientDefine;
import com.fairytask.extention.define.SceneDefine;
import com.fairytask.extention.scene.SceneManager;
import com.fairytask.extention.user.UserManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class UserRegisterHandler extends BaseClientRequestHandler
{

	private UserManager userMgr;
	private LoginExtention	loginExt;
	
	public UserRegisterHandler()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	public void handleClientRequest(User user, ISFSObject pInfo)
	{
		loginExt	=	(LoginExtention)getParentExtension();
		userMgr	=	new UserManager(loginExt);
		//先检查用户名能不能用
		try
		{
			//用户名不存在
			if(userMgr.checkNick(pInfo.getUtfString("nickname")))
			{
				userMgr.userRegister(pInfo, user);
				//取得 API
				SceneManager sceneMgr	=	new SceneManager(user, getApi(),loginExt);
				sceneMgr.switchScene(SceneDefine.ROOM_LIST);
			}
			else
			{
				//存在的话 告诉一下前台
				loginExt.send(CustomToClientDefine.USER_DUP_NAME, null, user);
			}
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
